﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using WinFormsAppTest.Dao;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsAppTest
{
    public partial class OvertimeAddForm : Form
    {
        //
        public String username = "null";//设置默认登录用户(手动添加)
        public int id = 0;//设置默认登录用户(手动添加)
        //
        public OvertimeAddForm()
        {
            InitializeComponent();
        }

        // -------有参 窗口（手动添加）--------
        public OvertimeAddForm(String name, int uid)
        {
            username = name;
            id = uid;
            InitializeComponent();
        }
        //-----------------------------------

        private void OKbutton_Click(object sender, EventArgs e)
        {
            AdminDao testdao = new AdminDao();
            OvertimeDao Overtimedao = new OvertimeDao();
            if (BegindateTimePicker.Value == DateTime.MinValue ||
                BegindateTimePicker.Value == null)
            {
                MessageBox.Show("加班开始时间不能为空！");
            }
            else if (EnddateTimePicker.Value == DateTime.MinValue ||
                     EnddateTimePicker.Value == null)
            {
                MessageBox.Show("加班结束时间不能为空！");
            }
            else if (WorkIdlabel.Text == "null")
            {
                MessageBox.Show("考勤编号不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(UserIdtextBox.Text))
            {
                MessageBox.Show("添加员工编号不能为空！");
            }
            if (int.TryParse(UserIdtextBox.Text, out int usernumber)
                && testdao.GetUserById(usernumber).Count != 0
                && int.TryParse(WorkIdlabel.Text, out int worknumber))
            {

                Overtimedao.CreateOvertime(BegindateTimePicker.Value, EnddateTimePicker.Value,
                                      worknumber, usernumber);
                MessageBox.Show("添加成功！");
                OvertimeForm f2 = new OvertimeForm(username, id);
                this.Hide();
                f2.ShowDialog();
                this.Dispose();
            }
            else
            {
                MessageBox.Show("添加员工编号不存在！");
            }
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            OvertimeForm f2 = new OvertimeForm(username, id);
            this.Hide();
            f2.ShowDialog();
            this.Dispose();
        }

        private void OvertimeAddForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }

        private void UserIdtextBox_TextChange(object sender, EventArgs e)
        {
            WorkIdlabel.Text = "null";
            WorkDao workDao = new WorkDao();
            if (int.TryParse(UserIdtextBox.Text, out int number))
            {
                if (workDao.GetWorkByUserId(number).Count != 0)
                {
                    int worknumber = workDao.GetWorkByUserId(number)[0].Workid;
                    WorkIdlabel.Text = worknumber.ToString();
                }
            }
        }

        private void OvertimeAddForm_Load(object sender, EventArgs e)
        {
            BegindateTimePicker.Format = DateTimePickerFormat.Custom;
            BegindateTimePicker.CustomFormat = "yyyy-MM-dd HH:mm";
            EnddateTimePicker.Format = DateTimePickerFormat.Custom;
            EnddateTimePicker.CustomFormat = "yyyy-MM-dd HH:mm";
        }
    }
}
